package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.StudentMapper;
import com.hqyj.pojo.Student;
import com.hqyj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p></p>
 *
 * @author : wwy
 * @date : 2020-09-03 09:18
 **/
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Override
    public List<com.hqyj.pojo.Student> Sall() {
        return null;
    }

    @Override
    public HashMap<String, Object> add(Student student) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(studentMapper.insertSelective(student)>0){
            map.put("info","新增成功");
        }else{
            map.put("info","新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Student student) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(studentMapper.updateByPrimaryKeySelective((com.hqyj.pojo.Student) student)>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(studentMapper.deleteByPrimaryKey(id)>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public Object select(Student student) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(student.getPage(),student.getRow());
        //2 查询结果集合
        List<Student> list = studentMapper.selectAll();
        // 3 创建分页对象
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);

        System.out.println("总条数="+pageInfo.getTotal());
        System.out.println("总页数="+pageInfo.getPages());
        System.out.println("上一页="+pageInfo.getPrePage());
        System.out.println("下一页="+pageInfo.getNextPage());
        System.out.println("首页="+pageInfo.getNavigateFirstPage());
        System.out.println("尾页="+pageInfo.getNavigateLastPage());
        System.out.println("当前页="+student.getPage());
        List<Student> listResult = pageInfo.getList();
        for(Student e:listResult){
            System.out.println(e.toString());
        }
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",student.getPage());
        map.put("data",pageInfo.getList());
        return map;
    }

}
